/*---------------------------------------------------------------------------*
 * Copyright (C) 2014 Ryan M. Murphy <ryan.m.murphy.77@gmail.com>
 *
 * File Name   : drv_chrg.h
 *
 * Project     : Watts-rock
 *               <http://code.google.com/p/watts-rock-motherboard/>
 *
 * Description : This driver is responsible for controlling the Watts-rock
 *               battery charge/discharge FETs, reading the voltage/current,
 *               monitoring the temperature, and charging the battery.
 *
 * References  : 1. AVR121: Enhancing ADC resolution by oversampling.
 *               2. INA170: High-Side, Bidirectional CURRENT SHUNT MONITOR
 * Last Update : Jan 4, 2014
 *---------------------------------------------------------------------------*/
#ifndef drv_chrg_h

   /*------------------------------------------------------------------------*
    * Global Defines
    *------------------------------------------------------------------------*/
   #define drv_chrg_h
   #define CHRG_DEVICE_ON  (1)
   #define CHRG_DEVICE_OFF (0)

   /*------------------------------------------------------------------------*
    * Include Files
    *------------------------------------------------------------------------*/
   #include "avr_compiler.h"
   #include "utl_adc.h"
   #include "utl_timer.h"

   /*------------------------------------------------------------------------*
    * Global Typedefs
    *------------------------------------------------------------------------*/
   typedef enum
   {

      CHRG_INVALID_ARG = -3, /*Invalid ioctl argument*/
      CHRG_INVALID_CMD = -2, /*Invalid ioctl command.*/
      CHRG_OUT_OF_HEAP = -1, /*No more memory.*/
      CHRG_PASSED      = 0   /*Configuration good.*/

   }t_chrgError; /*Possible error conditions returned by the device's
                  ioctl routine*/

   /*------------------------------------------------------------------------*
    * This data type defines the list of ioctl commands.
    *------------------------------------------------------------------------*/
   typedef enum
   {
      CHRG_STORE_CAL = 0,         /*Store voltage and current calibration
                                   factors*/
      CHRG_GET_MEAS,              /*Grab a voltage and current measurement*/
      CHRG_AC_CHRG_CNTRL,         /*Turn on/off the AC charge source*/
      CHRG_DC_CHRG_CNTRL,         /*Turn on/off the DC charge source*/
      CHRG_CHARGER_CNTRL,         /*Turn on/off the charge FET*/
      CHRG_PWR_CNTRL,             /*Turn on/off the power to the motherboard*/
      CHRG_FAN_CNTRL,             /*Turn on/off the fan*/
      CHRG_CHARGER_SET_DUTY_CYCLE /*Sets the PWM duty cycle*/
   }t_chrgCmd;

   /*------------------------------------------------------------------------*
    * This data type defines how data is returned from the device when
    * performing a voltage, current, and temperature measurement.
    *------------------------------------------------------------------------*/
   typedef struct
   {
      /*---------------------------------------------------------------------*
       * The measured battery voltage.
       *---------------------------------------------------------------------*/
      float f_volts;
      /*---------------------------------------------------------------------*
       * The instantaneous measured battery current (in Amps).
       *---------------------------------------------------------------------*/
      float f_current;
      /*---------------------------------------------------------------------*
       * The average battery current (in Amps).
       *---------------------------------------------------------------------*/
      float f_avrCurrent;
      /*---------------------------------------------------------------------*
       * The measured Sepic input voltage.
       *---------------------------------------------------------------------*/
      float f_sepicVolts;
      /*---------------------------------------------------------------------*
       * The measured Sepic module temperature (in degrees C).
       *---------------------------------------------------------------------*/
      float f_temp;
      /*---------------------------------------------------------------------*
       * If true, an AC signal has been connected to the charger.
       *---------------------------------------------------------------------*/
      bool b_acChargeDet;
      /*---------------------------------------------------------------------*
       * If true, a DC signal has been connected to the charger.
       *---------------------------------------------------------------------*/
      bool b_dcChargeDet;
   }t_chrgMeas;

   /*------------------------------------------------------------------------*
    * This data type defines how the voltage, current, and temperature
    * calibration factors are passed into the device driver.
    *------------------------------------------------------------------------*/
   typedef struct
   {
      /*---------------------------------------------------------------------*
       * The measured bias on the 'volt meter' ADC input.
       *---------------------------------------------------------------------*/
      float f_calVltMtrBias;
      /*---------------------------------------------------------------------*
       * The measured bias on the 'current meter' ADC input.
       *---------------------------------------------------------------------*/
      float f_calCurMtrBias;
      /*---------------------------------------------------------------------*
       * The measured bias on the Sepic input voltage measurement.
       *---------------------------------------------------------------------*/
      float f_calSepicVltMtrBias;
      /*---------------------------------------------------------------------*
       * The measured bias on the 'temperature meter' ADC input.
       *---------------------------------------------------------------------*/
      float f_calTempMtrBias;
      /*---------------------------------------------------------------------*
       * The measured resistance of the 'volt meter' voltage divider- in ohms.
       *---------------------------------------------------------------------*/
      float f_calVltMtrRDivide;
      /*---------------------------------------------------------------------*
       * The measured resistance of the Sepic input voltage voltage divider-
       * in ohms.
       *---------------------------------------------------------------------*/
      float f_calSepicVltMtrRDivide;
      /*---------------------------------------------------------------------*
       * The measured resistance of the 'current meter' R sense resister
       * - in ohms.
       *---------------------------------------------------------------------*/
      float f_calRsense;
   }t_chrgCal;

   typedef struct
   {
      /*---------------------------------------------------------------------*
       * Configures the appropriate ADC for the battery voltage measurement.
       *---------------------------------------------------------------------*/
      t_adcModuleId t_battVoltAdc;
      /*---------------------------------------------------------------------*
       * Configures the appropriate positive pin for the battery voltage
       * measurement.
       *---------------------------------------------------------------------*/
      uint8_t c_battVoltPosPin;
      /*---------------------------------------------------------------------*
       * Configures the appropriate negative pin for the battery voltage
       * measurement.
       *---------------------------------------------------------------------*/
      uint8_t c_battVoltNegPin;
      /*---------------------------------------------------------------------*
       * Configures the appropriate ADC for the battery current measurement.
       *---------------------------------------------------------------------*/
      t_adcModuleId t_battCurrAdc;
      /*---------------------------------------------------------------------*
       * Configures the appropriate positive pin for the battery current
       * measurement.
       *---------------------------------------------------------------------*/
      uint8_t c_battCurrPosPin;
      /*---------------------------------------------------------------------*
       * Configures the appropriate negative pin for the battery current
       * measurement.
       *---------------------------------------------------------------------*/
      uint8_t c_battCurrNegPin;
      /*---------------------------------------------------------------------*
       * Configures the appropriate ADC for the  temperature
       * measurement.
       *---------------------------------------------------------------------*/
      t_adcModuleId t_tempAdc;
      /*---------------------------------------------------------------------*
       * Configures the appropriate positive pin for the CHRG module
       * temperature measurement.
       *---------------------------------------------------------------------*/
      uint8_t c_tempPosPin;
      /*---------------------------------------------------------------------*
       * Configures the appropriate negative pin for the CHRG module
       * temperature measurement.
       *---------------------------------------------------------------------*/
      uint8_t c_tempNegPin;
      /*---------------------------------------------------------------------*
       * Configures the appropriate ADC for the Sepic input voltage
       * measurement.
       *---------------------------------------------------------------------*/
      t_adcModuleId t_sepicVoltAdc;
      /*---------------------------------------------------------------------*
       * Configures the appropriate positive pin for the Sepic input
       * voltage measurement.
       *---------------------------------------------------------------------*/
      uint8_t c_sepicVoltPosPin;
      /*---------------------------------------------------------------------*
       * Configures the appropriate negative pin for the Sepic input
       * voltage measurement.
       *---------------------------------------------------------------------*/
      uint8_t c_sepicVoltNegPin;
      /*---------------------------------------------------------------------*
       * Configures the appropriate PORT for detecting that an AC or DC signal
       * has been connected to the charger port.
       *---------------------------------------------------------------------*/
      uint8_t c_chargeDetPort;
      /*---------------------------------------------------------------------*
       * Configures the appropriate pin for detecting that an AC signal has
       * been attached to the charger port.
       *---------------------------------------------------------------------*/
      uint8_t c_acChargeDetPin;
      /*---------------------------------------------------------------------*
       * Configures the appropriate pin for detecting that an AC signal has
       * been attached to the charger port.
       *---------------------------------------------------------------------*/
      uint8_t c_dcChargeDetPin;
      /*---------------------------------------------------------------------*
       * Configures the appropriate PORT for controlling the charge FET.
       *---------------------------------------------------------------------*/
      uint8_t c_chargeFetPort;
      /*---------------------------------------------------------------------*
       * Configures the appropriate pin for controlling the charge FET.
       *---------------------------------------------------------------------*/
      uint8_t c_chargeFetPin;
      /*---------------------------------------------------------------------*
       * Configures the appropriate timer for controlling the charge FET.
       *---------------------------------------------------------------------*/
      t_timerModId t_chargeFetTimer;
      /*---------------------------------------------------------------------*
       * Configures the appropriate compare channel for the charge FET timer.
       *---------------------------------------------------------------------*/
      t_compType t_chargeFetCompType;
      /*---------------------------------------------------------------------*
       * The port that controls turning on/off power to the CHRG module.
       *---------------------------------------------------------------------*/
      uint8_t c_shutdownPort;
      /*---------------------------------------------------------------------*
       * Configures the appropriate pin for turning on/off power to the
       * CHRG module.
       *---------------------------------------------------------------------*/
      uint8_t c_shutdownPin;
      /*---------------------------------------------------------------------*
       * The port that controls turning on/off power to the fan.
       *---------------------------------------------------------------------*/
      uint8_t c_fanPort;
      /*---------------------------------------------------------------------*
       * Configures the appropriate pin for turning on/off power to the
       * fan.
       *---------------------------------------------------------------------*/
      uint8_t c_fanPin;
      /*---------------------------------------------------------------------*
       * The port that controls turning on/off the source for charging via
       * an AC connection.
       *---------------------------------------------------------------------*/
      uint8_t c_acChrgSourcePort;
      /*---------------------------------------------------------------------*
       * Configures the appropriate pin for turning on/off the AC charge
       * source.
       *---------------------------------------------------------------------*/
      uint8_t c_acChrgSourcePin;
      /*---------------------------------------------------------------------*
       * The port that controls turning on/off the source for charging via
       * an DC connection.
       *---------------------------------------------------------------------*/
      uint8_t c_dcChrgSourcePort;
      /*---------------------------------------------------------------------*
       * Configures the appropriate pin for turning on/off the DC charge
       * source.
       *---------------------------------------------------------------------*/
      uint8_t c_dcChrgSourcePin;
      /*---------------------------------------------------------------------*
       * This value represents a unique number assigned to this driver and has
       * to be different from all others registered with the kernel.
       *---------------------------------------------------------------------*/
      uint8_t c_majorNum;

   }t_chrgSetup;

   /*------------------------------------------------------------------------*
    * Global Variables
    *------------------------------------------------------------------------*/

   /*------------------------------------------------------------------------*
    * Global Inline functions
    *------------------------------------------------------------------------*/

   /*------------------------------------------------------------------------*
    * Global Function Prototypes
    *------------------------------------------------------------------------*/
   t_error drv_chrgInit( t_chrgSetup t_setup);

   void drv_chrgExit( void);

#endif/*End #ifndef drv_chrg_h*/
